import { defineStore } from 'pinia'

export const useProjectStore = defineStore('project', {
  state: () => ({
    projectList: [],
    currentProject: null,
    categories: [],
    filters: {
      category: '',
      budget: '',
      date: '',
      location: ''
    },
    pagination: {
      page: 1,
      pageSize: 10,
      total: 0
    }
  }),
  
  getters: {
    getProjectList: (state) => state.projectList,
    getCurrentProject: (state) => state.currentProject,
    getCategories: (state) => state.categories,
    getFilters: (state) => state.filters
  },
  
  actions: {
    async fetchProjects(params = {}) {
      try {
        const res = await uni.request({
          url: '/api/projects',
          method: 'GET',
          data: {
            page: this.pagination.page,
            pageSize: this.pagination.pageSize,
            ...this.filters,
            ...params
          }
        })
        
        if (res.data.code === 0) {
          this.projectList = res.data.data.list
          this.pagination.total = res.data.data.total
          return true
        }
        return false
      } catch (error) {
        console.error('Fetch projects failed:', error)
        return false
      }
    },
    
    async fetchProjectDetail(id) {
      try {
        const res = await uni.request({
          url: `/api/projects/${id}`,
          method: 'GET'
        })
        
        if (res.data.code === 0) {
          this.currentProject = res.data.data
          return true
        }
        return false
      } catch (error) {
        console.error('Fetch project detail failed:', error)
        return false
      }
    },
    
    async publishProject(data) {
      try {
        const res = await uni.request({
          url: '/api/projects/publish',
          method: 'POST',
          data
        })
        
        if (res.data.code === 0) {
          return true
        }
        return false
      } catch (error) {
        console.error('Publish project failed:', error)
        return false
      }
    },
    
    async submitBid(projectId, data) {
      try {
        const res = await uni.request({
          url: `/api/projects/${projectId}/bid`,
          method: 'POST',
          data
        })
        
        if (res.data.code === 0) {
          return true
        }
        return false
      } catch (error) {
        console.error('Submit bid failed:', error)
        return false
      }
    },
    
    async fetchCategories() {
      try {
        const res = await uni.request({
          url: '/api/categories',
          method: 'GET'
        })
        
        if (res.data.code === 0) {
          this.categories = res.data.data
          return true
        }
        return false
      } catch (error) {
        console.error('Fetch categories failed:', error)
        return false
      }
    },
    
    setFilters(filters) {
      this.filters = {
        ...this.filters,
        ...filters
      }
    },
    
    resetFilters() {
      this.filters = {
        category: '',
        budget: '',
        date: '',
        location: ''
      }
    },
    
    setPage(page) {
      this.pagination.page = page
    },
    
    resetPage() {
      this.pagination.page = 1
    }
  }
}) 